package com.wcl.demo.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.easemob.chat.EMChatManager;
import com.wcl.demo.R;
import com.wcl.demo.db.AroundHelper;
import com.wcl.demo.model.entity.AroundInfor;
import com.wcl.demo.network.VolleySingleton;
import com.wcl.demo.ui.activity.BachgroundDownloadActivity;
import com.wcl.demo.ui.activity.ChosePhotoActivity;
import com.wcl.demo.ui.activity.MineHelpActivity;
import com.wcl.demo.ui.activity.MinePrivateActivity;
import com.wcl.demo.ui.activity.NoteActivity;
import com.wcl.demo.ui.activity.SearchWeatherActivity;
import com.wcl.demo.ui.activity.SetActivity;
import com.wcl.demo.ui.activity.UserInforActivity;
import com.wcl.demo.utils.ActivityUtils;
import com.wcl.demo.utils.PreferenceManager;

import java.util.List;


/**
 * Created by dllo on 15/11/18.
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    private RelativeLayout mine_fragment_setItem, fragment_friend_my_note, mine_help_relativeLayout,
            mine_fragment_change_chatBg, mine_set_relativeLayout,
            mine_fregment_search_weather_relativelayout, background_downloadRl;

    private TextView person_material, fragment_note_noteNumberTv, fragment_mine_head_nicknameIV, fragment_mine_head_mine_idIV;
    private AroundHelper helper;
    private List<AroundInfor> list;
    private ImageView fragment_mine_notePic1, fragment_mine_notePic2, fragment_mine_notePic3, fragment_mine_head_pictureIV;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();//定义Volley请求
    private String username = EMChatManager.getInstance().getCurrentUser();//定义当前用户名

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, null);//加载布局
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mine_set_relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.mine_set_relativeLayout);
        mine_set_relativeLayout.setOnClickListener(this);//设置
        mine_help_relativeLayout = (RelativeLayout) getActivity().findViewById(R.id.mine_help_relativeLayout);
        mine_help_relativeLayout.setOnClickListener(this);//帮助
        background_downloadRl = (RelativeLayout) getActivity().findViewById(R.id.background_downloadRl);
        background_downloadRl.setOnClickListener(this);//下载相关壁纸
        mine_fragment_setItem = (RelativeLayout) getActivity().findViewById(R.id.mine_fragment_setItem);
        mine_fragment_setItem.setOnClickListener(this);//设置
        person_material = (TextView) getActivity().findViewById(R.id.person_material);
        person_material.setOnClickListener(this);//资料
        //用户头像
        fragment_mine_head_mine_idIV = (TextView) getActivity().findViewById(R.id.fragment_mine_head_mine_idIV);
        //用户昵称
        fragment_mine_head_nicknameIV = (TextView) getActivity().findViewById(R.id.fragment_mine_head_nicknameIV);
        //用户头像
        fragment_mine_head_pictureIV = (ImageView) getActivity().findViewById(R.id.fragment_mine_head_pictureIV);
        fragment_friend_my_note = (RelativeLayout) getActivity().findViewById(R.id.fragment_friend_my_note);
        fragment_friend_my_note.setOnClickListener(this); //心情笔记
        mine_fregment_search_weather_relativelayout = (RelativeLayout) getActivity().
                findViewById(R.id.mine_fregment_search_weather_relativelayout);
        mine_fregment_search_weather_relativelayout.setOnClickListener(this);//查询天气
        mine_fragment_change_chatBg = (RelativeLayout) getActivity().findViewById(R.id.mine_fragment_change_chatBg);
        mine_fragment_change_chatBg.setOnClickListener(this);//更换头像背景墙

        fragment_note_noteNumberTv = (TextView) getActivity().findViewById(R.id.fragment_note_noteNumberTv);
        initData();

        //noteItem的三个头像
        fragment_mine_notePic1 = (ImageView) getActivity().findViewById(R.id.fragment_mine_notePic1);
        fragment_mine_notePic2 = (ImageView) getActivity().findViewById(R.id.fragment_mine_notePic2);
        fragment_mine_notePic3 = (ImageView) getActivity().findViewById(R.id.fragment_mine_notePic3);
    }

    //三张图片加数据
    @Override
    public void onResume() {
        super.onResume();
        setNoteItemPicture();
        fragment_note_noteNumberTv.setText(list.size() + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_fragment_setItem://设置
                Intent intent = new Intent(getActivity(), SetActivity.class);
                startActivity(intent);
                break;
            case R.id.person_material://个人资料
                Bundle bundle = new Bundle();
                bundle.putString("currentusername", username);
                ActivityUtils.startToActivity(getActivity(), UserInforActivity.class, bundle);
                break;
            case R.id.fragment_friend_my_note://心情笔记
                Bundle bundle_node = new Bundle();
                bundle_node.putString("currentusername", username);
                ActivityUtils.startToActivity(getActivity(), NoteActivity.class, bundle_node);
                break;
            case R.id.mine_fregment_search_weather_relativelayout://天气查询
                ActivityUtils.startToActivity(getActivity(), SearchWeatherActivity.class);
                break;
            case R.id.mine_fragment_change_chatBg://聊天背景
                Intent intent_chosePhoto = new Intent(getActivity(), ChosePhotoActivity.class);
                intent_chosePhoto.putExtra("chosePicture_key", "2");//调用选图的方法"2"
                getActivity().startActivity(intent_chosePhoto);
                break;
            case R.id.mine_set_relativeLayout://名片设置
                Intent intent_private_set = new Intent(getActivity(), MinePrivateActivity.class);
                getActivity().startActivity(intent_private_set);
                break;
            case R.id.mine_help_relativeLayout://帮助
                Intent intent_help = new Intent(getActivity(), MineHelpActivity.class);
                getActivity().startActivity(intent_help);
                break;
            case R.id.background_downloadRl://背景相关下载
                Intent intent_download = new Intent(getActivity(), BachgroundDownloadActivity.class);
                getActivity().startActivity(intent_download);
                break;
        }
    }

    private void initData() {
        helper = new AroundHelper();
        list = helper.queryAllOrder();

        fragment_mine_head_nicknameIV.setText(username);//设置当前用户名
        fragment_mine_head_mine_idIV.setText(getCurrentUserNick());//设置当前用户昵称
        getBitmap(getCurrentUserAvatar(), fragment_mine_head_pictureIV);//设置头像
    }

    //心情日记Item的三张图片
    private void setNoteItemPicture() {
        int j = 1;
        initData();
        for (int i = 0; list.size() > i && i < 3; i++) {
            switch (j) {
                case 1:
                    fragment_mine_notePic1.setImageBitmap(BitmapFactory.decodeFile(list.get(i).getPictureKey()));
                    break;
                case 2:
                    fragment_mine_notePic2.setImageBitmap(BitmapFactory.decodeFile(list.get(i).getPictureKey()));
                    break;
                case 3:
                    fragment_mine_notePic3.setImageBitmap(BitmapFactory.decodeFile(list.get(i).getPictureKey()));
                    break;
            }
            j += 1;
        }

    }

    /****
     * 从缓存中获取用户昵称
     * @return
     */
    private String getCurrentUserNick() {
        return PreferenceManager.getInstance().getCurrentUserNick();
    }

    /***
     * 从缓存中获取用户头像
     * @return
     */
    private String getCurrentUserAvatar() {
        return PreferenceManager.getInstance().getCurrentUserAvatar();
    }


    /***
     * 图片请求
     *
     * @param bitmap
     * @param imageView
     */
    private void getBitmap(String bitmap, final ImageView imageView) {
        volleySingleton.getImageResuest(bitmap, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }

}
